<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>英雄管理</el-breadcrumb-item>
      <el-breadcrumb-item>装备管理</el-breadcrumb-item>
      <el-breadcrumb-item>装备列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-table :data="equipList" style="width: 100%" stripe border>
        <el-table-column type="index" label="#" width="width"></el-table-column>
        <el-table-column prop="_id" label="ID" width="width"></el-table-column>
        <el-table-column label="装备名称" width="width" prop="name"></el-table-column>
        <el-table-column label="装备小图片" width="width">
          <template slot-scope="scope">
            <img :src="scope.row.image" style="width:50px;height:50px" />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="width">
          <template slot-scope="scope">
            <el-button
              type="success"
              size="small"
              icon="el-icon-edit"
              @click="editEquip(scope.row)"
            >编辑</el-button>
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="removeEquip(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="编辑装备" :visible.sync="equipEditVisible" width="30%">
      <el-form>
        <el-form-item label="装备名称">
          <el-input style="width:250px" v-model="equipEditList.name"></el-input>
        </el-form-item>
        <el-form-item label="上传图标">
          <el-upload
            class="avatar-uploader"
            :action="$http.defaults.baseURL + '/upload'"
            :show-file-list="false"
            :on-success="afterUpload"
            :headers="getAuthHeaders()"
            name="img"
          >
            <img v-if="equipEditList.image" :src="equipEditList.image" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="equipEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSaveEquip">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'EquipList',
  data() {
    return {
      equipList: [],
      equipEditVisible: false,
      equipEditList: {}
    }
  },
  methods: {
    // 获取列表数据
    async getEquipList() {
      const res = await this.$http.get('rest/equips')
      this.equipList = res.data
      console.log(res)
    },
    // 保存到数据库
    async editSaveEquip() {
      await this.$http.put(
        `rest/equips/${this.equipEditList._id}`,
        this.equipEditList
      )
      this.$message.success(`成功编辑装备:${this.equipEditList.name}`)
      this.getEquipList()
      this.equipEditVisible = false
    },
    editEquip(row) {
      this.equipEditVisible = true
      this.equipEditList = row
    },
    afterUpload(res) {
      this.$set(this.equipEditList, 'image', res.url)
    },
    // 删除
    async removeEquip(row) {
      const confirm = await this.$confirm(
        `是否删除装备:${row.name}`,
        '删除操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      if (confirm === 'confirm') {
        await this.$http.delete(`rest/equips/${row._id}`)
        this.$message.success(`成功删除装备：${row.name}`)
        this.getEquipList()
      } else {
        this.$message.info('取消删除操作')
      }
    }
  },
  created() {
    this.getEquipList()
  }
}
</script>